<?php
namespace Nawa;
/**
 * Nawa MedicalStudy.
 *
 * This object represent a Medical Study.
 *
 *
 * void @method customer()
 */
use Nawa\MedicalStudy\Followup;
use Fox\Dao;

class MedicalStudy extends \Fox\Object\Versioned
{
	public static $JOIN_PARTNERS = array(
		'source' => 'mss',
		'table' => 'ms_partners',
		'short' => 'pa',
		'foreign' => 'msid',
		'column' => 'msid',
		'type' => 'INNER JOIN');

	/**
	 * Join Customers
	 * @var array
	 */
	public static $JOIN_CUSTOMERS = array(
		'source' => 'customers',
		'table' => 'ms_partners',
		'short' => 'pa',
		'foreign' => 'customerid',
		'column' => 'customerid',
		'type' => 'INNER JOIN');
	
	/**
	 * Link to partners
	 */
	const LINK_PARTNERS = 'partners';

	/**
	 * Link to followup
	 */
	const LINK_FOLLOWUP = 'followup';
	
	/**
	 * Link to customer
	 */
	const LINK_CUSTOMER = 'customer';

	/**
	 * Link to partners
	 */
	const LINK_MEDICAL_CENTERS_PARTNERS = 'medicalCentersPartners';

	/**
	 * Link to Metadatas
	 */
	const LINK_METADATAS = 'metadatas';
	
	/**
	 * 
	 * Link to partners type admnistrators
	 * @var string
	 */
	const LINK_ADMINISTRATORS = 'administrators';
	
	/**
	 * New Status
	 * @var int
	 */
	const STATUS_NEW = '0';

	/**
	 * Open Status
	 * @var int
	 */
	const STATUS_OPEN = '1';

	/**
	 * Close Status
	 * @var int
	 */
	const STATUS_CLOSE = '4';

	/**
	 * Standard Medical Study
	 */
	const TYPE_BASIC = '1';

	/**
	 * Role Customer
	 * @var string
	 */
	const ROLE_CUSTOMER = 'AG';

	/**
	 * Role Creator of the MS
	 * @var string
	 */
	const ROLE_ADMIN = 'ADM';

	/**
	 * Role Medical Center
	 * @var string
	 */
	const ROLE_MEDICAL_CENTERS = 'MC';

	/**
	 * Change add partners
	 * @var string
	 */
	const CHANGE_ADD_PARTNERS = 'add-partners';
	
	/**
	 * Change add a document to a partner
	 * @var string
	 */
	const CHANGE_ADD_DOCUMENT_TO_PARTNER = 'add-partner-doc';
	
	/**
	 * Change add medical centers
	 * @var string
	 */
	const CHANGE_ADD_MEDICAL_CENTERS = 'add-mcs';
	
	/**
	 * Change add a document to a partner
	 * @var string
	 */
	const CHANGE_ADD_FOLLOWUP_ITEM = 'add-followup';
		
	/**
	 * Medical Study Domain for checklists
	 * @var int
	 */
	const DOMAIN_MEDICAL_STUDY = 1;
	
	/**
	 *
	 * Customizing Object
	 * @var MedicalStudy\Customizing
	 */
	private $followup = null;

	/**
	 * (non-PHPdoc)
	 * @see Fox.Object::getData()
	 */
	public function getData()
	{
		$data = parent::getData();
		//$data = array_merge($data,$this->followup()->getData());
		return $data;
	}

	/**
	 * 
	 * Find all MS admnistrators
	 * @return array
	 */
	public function findAdministrators() {
		$administrators = array();
		
		foreach($this->partners() as $partner) {
			if($partner->role == MedicalStudy\Authorization::ROLE_ADMIN) {
				$user = $partner->user();
				if($user) {
					$administrators[$user->getId()] = $user;
				}
			}
		}
		// Add MS Creator
		if(!isset($administrators[$this->cname])) {
			$administrators[$this->cname] = \Fox\Dao::load(\User::className(),$this->cname);
		}
		return $administrators;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/\Fox\Object\Versioned::afterVersionCreate()
	 */
	protected function afterVersionCreate(\Fox\Version $version)
	{
		// Add Resume to the search engine
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->add($this,$this->getId(),self::className());
	}

	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/\Fox\Object\Versioned::afterVersionUpdate()
	 */
	protected function afterVersionUpdate()
	{
		// Search Engine
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->synchronize($this,$this->getId(),self::classname());
	}
	
	/**
	 * Find a specific metadata 
	 * @return object
	 */
	public function metadata($name) {
		return $this->findMetadatas($this->getInLineId().':'.$name);
	}

}
\Fox\Dao::infos(MedicalStudy::className(),
new \Fox\Object\Info('mss', \Fox\Databases::DB_APP)
);

// Link to Partners
\Fox\Object\Links::add(\Fox\Dao::link(MedicalStudy\Partner::className(), MedicalStudy\Partner::LINK_MEDICAL_STUDY)
->to(MedicalStudy::className(), MedicalStudy::LINK_PARTNERS)
->on('msid')
->versionFrom());

// Link to Medical Centers Partners
\Fox\Object\Links::add(\Fox\Dao::link(MedicalStudy\MedicalCenter::className(), MedicalStudy\MedicalCenter::LINK_MEDICAL_STUDY)
->to(MedicalStudy::className(), MedicalStudy::LINK_MEDICAL_CENTERS_PARTNERS)
->on('msid')
->versionFrom());

// Link to followup
\Fox\Object\Links::add(\Fox\Dao::link(MedicalStudy\Followup::className(), MedicalStudy\Followup::LINK_MEDICAL_STUDY)
->to(MedicalStudy::className(), MedicalStudy::LINK_FOLLOWUP)
->on('msid')
->orderBy('position')
->versionFrom());

// Link to Metadatas
\Fox\Object\Links::add(\Fox\Dao::link(MedicalStudy\Metadata::className(), MedicalStudy\Metadata::LINK_MEDICAL_STUDY)
->to(MedicalStudy::className(), MedicalStudy::LINK_METADATAS)
->on('msid')
->versionFrom());

// Link to Customer
\Fox\Object\Links::add(\Fox\Dao::link(MedicalStudy::className(), MedicalStudy::LINK_CUSTOMER)
->to(\Nawa\Customer::className(), \Nawa\Customer::LINK_MEDICAL_STUDIES)
->on('customerid')
->oneToOne());